home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 106_01.zip / STDLIB1.C < prev    next >
Text File  |  1993-06-26  |  7KB  |  379 lines

  1. /*    ------------------------------------------------------------
  2.     Mods by H Moran:
  3.  
  4.     In case you wish to use some but not all of the mods,
  5.     lines modified are marked with HRM mod and
  6.     lines added are marked with HRM
  7.     This is not a plea for recognition, simply a convient marker.
  8.  
  9.         1) add: recogintion of list reader and punch devices
  10.            to getc(), putc() and fflush()
  11.         2) add: fill remaining buffer with 0x1a's to fflush()
  12.         3) add new function ungetc() to back up the pointer
  13.            of a buffered file. This will not back up
  14.            past the 1'st char in the buffer.
  15.  
  16.     -------------------------------------------------------------
  17. */
  18.  
  19.  
  20. /*
  21.  
  22.     The files STDLIB*.C contain source for all DEFF.CRL
  23.     functions which are written in C; Any functions which
  24.     appear in DEFF.CRL but have no corresponding source
  25.     were written in machine code and hand-converted to
  26.     .CRL format (as described in the User's Guide.)
  27.     Conversely, any functions whose source appears
  28.     in one of these files but for which there are no
  29.     entries in DEFF.CRL must be compiled before using.
  30.     There wasn't enough space in the DEFF.CRL directory
  31.     for all of them, and this package has got enough
  32.     separate files as it is.
  33.     All functions written by Leor Zolman....who is
  34.     soley responsible for their kludginess.
  35.  
  36.     Functions appearing in this file:
  37.  
  38.     fopen        getc        getw
  39.     fcreat        putc        putw
  40.     fflush
  41.     atoi
  42.     strcat        strcmp        strcpy        strlen
  43.     isalpha        isupper        islower        isdigit
  44.     isspace        toupper        tolower
  45.     qsort *
  46.     initw        initb        getval
  47.     abs
  48.  
  49.     *) re-written for version 1.31. Now sorts in ASCENDING order
  50.        instead of the previous DESCENDINGG; also, a shell sort
  51.        algorithm is used instead of the incredibly inefficient
  52.        previous bubble-sort technique.
  53.  
  54. */
  55.  
  56. /* Buffered I/O for C  */
  57.  
  58. struct buf {
  59.     int fd;
  60.     int nleft;
  61.     char *nextp;
  62.     char buff[128];
  63.  };
  64.  
  65.  
  66. fopen(filename,iobuf)
  67. struct buf *iobuf;
  68. char *filename;
  69. {
  70.     int fd2;
  71.     if ((fd2= open(filename,0))<0) return -1;
  72.     iobuf -> fd = fd2;
  73.     iobuf -> nleft = 0;
  74.     return fd2;
  75. }
  76.  
  77.  
  78. getc(iobuf)
  79. struct buf *iobuf;
  80. {
  81.     if (iobuf==0) return getchar();    /* HRM console */
  82.     if (iobuf==3) return bdos(3);    /* HRM reader device */
  83.     if (iobuf -> nleft--) return *iobuf -> nextp++;
  84.     if ((read(iobuf -> fd, iobuf -> buff, 1)) <= 0)
  85.                 return -1;
  86.     iobuf -> nleft = 127;
  87.     iobuf -> nextp = iobuf -> buff;
  88.     return *iobuf -> nextp++;
  89. }
  90.  
  91.  
  92. /*    function added by HRM
  93.     Back up nextp and nleft, return nleft
  94.     if we are already at the 1'st char (nleft==128)  
  95.     then return 0 as an error indicator.
  96.     This error indicator was picked to allow calls
  97.     of the form:
  98.             if ( ungetc(iobuf) ) <handle the error>
  99.  
  100.     The error can never happen if only 1 invocation
  101.     of ungetc() is made between invocations of getc()
  102. */
  103.  
  104. ungetc(iobuf)
  105. struct buf *iobuf;
  106. {
  107.     if (iobuf < 4) return 0;
  108.     if (iobuf -> nleft == 128) return 0;
  109.     iobuf -> nextp--;
  110.     return ++(iobuf -> nleft);
  111. }
  112.  
  113.  
  114. getw(iobuf)
  115. struct buf *iobuf;
  116. {
  117.     int a,b;    
  118.     if (((a=getc(iobuf)) >= 0) && ((b= getc(iobuf)) >=0))
  119.             return 256*b+a;
  120.     return -1;
  121. }
  122.  
  123.  
  124. fcreat(name,iobuf)
  125. char *name;
  126. struct buf *iobuf;
  127. {
  128.     int fd2;
  129.     unlink(name);
  130.     if ((fd2 = creat(name))<0 ) return -1;
  131.     iobuf -> fd = fd2;
  132.     iobuf -> nextp = iobuf -> buff;
  133.     iobuf -> nleft = 128;
  134.     return fd2;
  135. }
  136.  
  137.  
  138. putc(c,iobuf)
  139. int c;
  140. struct buf *iobuf;
  141. {
  142.     if (iobuf == 1) return putchar(c);    /* HRM console */
  143.     if (iobuf == 2) return bdos(5,c);    /* HRM list device */
  144.     if (iobuf == 3) return bdos(4,c);    /* HRM punch device */
  145.     if (iobuf -> nleft--) return *iobuf->nextp++ =c;
  146.     if ((write(iobuf -> fd, iobuf -> buff,1)) != 1)
  147.             return -1;
  148.     iobuf -> nleft = 127;
  149.     iobuf -> nextp = iobuf -> buff;
  150.     return *iobuf -> nextp++ = c;
  151. }
  152.  
  153.  
  154. putw(w,iobuf)
  155. unsigned w;
  156. struct buf *iobuf;
  157. {
  158.     if ((putc(w%256,iobuf) >=0)&&(putc(w/256,iobuf)>=0))
  159.                 return w;
  160.     return -1;
  161. }
  162.  
  163.  
  164. fflush(iobuf)
  165. struct buf *iobuf;
  166. {
  167. int cnt;
  168. char *fill;
  169.  
  170.     if (iobuf < 4) return 0;    /* HRM mod non-disk device */
  171.     if (iobuf -> nleft == 128) return 0;
  172.     for( fill=iobuf->nextp,cnt=iobuf->nleft; cnt; cnt-- )    /* HRM */
  173.       *fill++ = 0x1a;                    /* HRM */
  174.     if ((write(iobuf -> fd, iobuf -> buff,1)) <=0)
  175.             return -1;
  176.     if (iobuf -> nleft != 0)
  177.         return seek(iobuf->fd, -1, 1);
  178.     iobuf -> nleft = 128;
  179.     iobuf -> nextp = iobuf -> buff;
  180.     return 0;
  181. }
  182.  
  183. /*
  184.     Some string functions
  185. */
  186.  
  187.  
  188. atoi(n)
  189. char *n;
  190. {
  191.     int val; 
  192.     char c;
  193.     int sign;
  194.     val=0;
  195.     sign=1;
  196.     while ((c = *n) == '\t' || c== ' ') ++n;
  197.     if (c== '-') {sign = -1; n++;}
  198.     while (  isdigit(c = *n++)) val = val * 10 + c - '0';
  199.     return sign*val;
  200. }
  201.  
  202.  
  203. strcat(s1,s2)
  204. char *s1, *s2;
  205. {
  206.     char *temp; temp=s1;
  207.     while(*s1) s1++;
  208.     do *s1++ = *s2; while (*s2++);
  209.     return temp;
  210. }
  211.  
  212.  
  213. strcmp(s,t)
  214. char s[], t[];
  215. {
  216.     int i;
  217.     i = 0;
  218.     while (s[i] == t[i])
  219.         if (s[i++] == '\0')
  220.             return 0;
  221.     return s[i] - t[i];
  222. }
  223.  
  224.  
  225. strcpy(s1,s2)
  226. char *s1, *s2;
  227. {
  228.     char *temp; temp=s1;
  229.     while (*s1++ = *s2++);
  230.     return temp;
  231. }
  232.  
  233.  
  234. strlen(s)
  235. char *s;
  236. {
  237.     int len;
  238.     len=0;
  239.     while (*s++) len++;
  240.     return(len);
  241. }
  242.  
  243.  
  244. /*
  245.     Some character diddling functions
  246. */
  247.  
  248. isalpha(c)
  249. char c;
  250. {
  251.     return isupper(c) || islower(c);
  252. }
  253.  
  254.  
  255. isupper(c)
  256. char c;
  257. {
  258.     return c>='A' && c<='Z';
  259. }
  260.  
  261.  
  262. islower(c)
  263. char c;
  264. {
  265.     return c>='a' && c<='z';
  266. }
  267.  
  268.  
  269. isdigit(c)
  270. char c;
  271. {
  272.     return c>='0' && c<='9';
  273. }
  274.  
  275.  
  276. isspace(c)
  277. char c;
  278. {
  279.     return c==' ' || c=='\t' || c=='\n';
  280. }
  281.  
  282.  
  283. char toupper(c)
  284. char c;
  285. {
  286.     return islower(c) ? c-32 : c;
  287. }
  288.  
  289.  
  290. char tolower(c)
  291. char c;
  292. {
  293.     return isupper(c) ? c+32 : c;
  294. }
  295.  
  296.  
  297. /*
  298.     Other stuff...
  299. */
  300.  
  301.  
  302. /*
  303.     This is the new qsort routine, utilizing the shell sort
  304.     technique given in the Software Tools book (by Kernighan 
  305.     & Plauger.)
  306.  
  307.     NOTE: this "qsort" function is different from the "qsort" given
  308.     in prior releases -- here, the items are sorted in ASCENDING
  309.     order. The old "qsort" sorted stuff in DESCENDING order, and
  310.     was in part responsible for the atrocious play of the "Othello"
  311.     program (it always made the WORST moves it could find...) 
  312. */
  313.  
  314. qsort(base, nel, width, compar)
  315. char *base; int (*compar)();
  316. {    int gap,ngap, i, j;
  317.     int jd, t1, t2;
  318.     t1 = nel * width;
  319.     for (ngap = nel / 2; ngap > 0; ngap /= 2) {
  320.        gap = ngap * width;
  321.        t2 = gap + width;
  322.        jd = base + gap;
  323.        for (i = t2; i <= t1; i += width)
  324.           for (j =  i - t2; j >= 0; j -= gap) {
  325.         if ((*compar)(base+j, jd+j) <=0) break;
  326.              _swp(width, base+j, jd+j);
  327.           }
  328.     }
  329. }
  330.  
  331. _swp(w,a,b)
  332. char *a,*b;
  333. int w;
  334. {
  335.     char tmp;
  336.     while(w--) {tmp=*a; *a++=*b; *b++=tmp;}
  337. }
  338.  
  339.  
  340.  
  341. /*
  342.      Initialization functions
  343. */
  344.  
  345.  
  346. initw(var,string)
  347. int *var;
  348. char *string;
  349. {
  350.     int n;
  351.     while ((n = getval(&string)) != -32760) *var++ = n;
  352. }
  353.  
  354. initb(var,string)
  355. char *var, *string;
  356. {
  357.     int n;
  358.     while ((n = getval(&string)) != -32760) *var++ = n;
  359. }
  360.  
  361. getval(strptr)
  362. char **strptr;
  363. {
  364.     int n;
  365.     if (!**strptr) return -32760;
  366.     n = atoi(*strptr);
  367.     while (**strptr && *(*strptr)++ != ',');
  368.     return n;
  369. }
  370.  
  371. /*
  372.     One lone little last function:
  373. */
  374.  
  375. abs(n)
  376. {
  377.     return (n<0) ? -n : n;
  378. }
  379.